<?php

//商品模型

class Goods_model extends S_Model {

	public function get_count_by_category($category_id = NULL)
	{
		if ( ! isset($category_id))
		{
			return $this->get_count();
		}
		else
		{
			return $this->get_count(array('category_id' => intval($category_id)));
		}
	}

	//返回商品列表分页数据 （当前页，每页数量）
	public function get_page_by_category($p, $per_page, $category_id = NULL)
	{
		
		$this->db->select('goods.id, goods.category_id, goods.name, goods.title, goods.sale_price, goods.stock, goods.on_sale, category.name as category_name');
		$this->db->from('goods');
		$this->db->join('category', 'goods.category_id = category.id', 'left');
		
		if (intval($category_id) > 0)
		{
			$this->db->where('goods.category_id', intval($category_id));
		}
		
		$this->db->limit($per_page, $per_page*($p - 1));
		$query = $this->db->get();
		$result = $query->result_array();
		$query->free_result();
		return $result;
	}

}